const Values = require('../template')

function testValuesAdvancedMethods() {
  // 1. valuesDeep 深度获取值（限制深度）
  console.log(
    "1. valuesDeep 深度获取值（限制深度）:",
    (() => {
      const obj = {
        a: 1,
        b: { c: 2, d: { e: 3 } },
        f: { g: 4 }
      }
      // 深度1：只取顶层值；深度2：取顶层+1层嵌套值
      const deep1Result = Values.valuesDeep(obj, 1)
      const deep2Result = Values.valuesDeep(obj, 2)

      const deep1Valid = [...deep1Result].sort().join(',') === '1,[object Object],[object Object]'
      const deep2Valid = [...deep2Result].sort((x, y) => x - y).join(',') === '1,2,4,[object Object]'
      return deep1Valid && deep2Valid
    })()
  )

  // 2. valuesWithFilter 条件过滤值（筛选大于2的值）
  console.log(
    "2. valuesWithFilter 条件过滤值（筛选大于2的值）:",
    (() => {
      const obj = { a: 1, b: 3, c: 2, d: 5, e: 0 }
      const predicate = (val) => val > 2
      const result = Values.valuesWithFilter(obj, predicate)
      const sortedResult = [...result].sort((x, y) => x - y)
      return sortedResult.join(',') === '3,5'
    })()
  )

  // 3. valuesWithType 类型过滤值（筛选字符串类型）
  console.log(
    "3. valuesWithType 类型过滤值（筛选字符串类型）:",
    (() => {
      const obj = { name: 'Alice', age: 25, isStudent: true, score: null }
      const result = Values.valuesWithType(obj, 'string')
      return result.length === 1 && result[0] === 'Alice'
    })()
  )

  // 4. valuesWithPattern 正则过滤值（筛选含"test"的字符串）
  console.log(
    "4. valuesWithPattern 正则过滤值（筛选含\"test\"的字符串）:",
    (() => {
      const obj = {
        key1: 'test123',
        key2: 'abc',
        key3: 'Test456',
        key4: 789
      }
      const pattern = /test/i // 不区分大小写匹配test
      const result = Values.valuesWithPattern(obj, pattern)
      const sortedResult = [...result].sort()
      return sortedResult.join(',') === 'Test456,test123'
    })()
  )

  // 5. valuesWithSort 排序获取值（数字降序）
  console.log(
    "5. valuesWithSort 排序获取值（数字降序）:",
    (() => {
      const obj = { a: 5, b: 2, c: 8, d: 1 }
      const compareFn = (x, y) => y - x
      const result = Values.valuesWithSort(obj, compareFn)
      return result.join(',') === '8,5,2,1'
    })()
  )

  // 6. valuesWithTransform 转换获取值（所有值乘2）
  console.log(
    "6. valuesWithTransform 转换获取值（所有值乘2）:",
    (() => {
      const obj = { a: 1, b: 3, c: 5 }
      const transformer = (val) => val * 2
      const result = Values.valuesWithTransform(obj, transformer)
      const sortedResult = [...result].sort((x, y) => x - y)
      return sortedResult.join(',') === '2,6,10'
    })()
  )

  // 7. valuesWithValidation 验证获取值（符合年龄范围18-30）
  console.log(
    "7. valuesWithValidation 验证获取值（符合年龄范围18-30）:",
    (() => {
      const obj = {
        user1: { age: 25 },
        user2: { age: 17 },
        user3: { age: 30 },
        user4: { age: 35 }
      }
      // 验证规则：age在18-30之间
      const schema = { age: { type: 'number', min: 18, max: 30 } }
      const result = Values.valuesWithValidation(obj, schema)
      const ages = result.map(user => user.age).sort((x, y) => x - y)
      return ages.join(',') === '25,30'
    })()
  )

  // 8. valuesWithStats 带统计信息的值获取
  console.log(
    "8. valuesWithStats 带统计信息的值获取:",
    (() => {
      const obj = { a: 1, b: 2, c: 3 }
      const statsResult = Values.valuesWithStats(obj)
      return statsResult.values.length === 3 &&
        statsResult.stats.count === 3 &&
        statsResult.stats.min === 1 &&
        statsResult.stats.max === 3 &&
        statsResult.stats.sum === 6
    })()
  )
}

// 执行测试
console.log("=== Values 高级方法测试 ===")
testValuesAdvancedMethods()
console.log("=== Values 高级方法测试完成 ===")
